package org.manusoft.refill.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.manusoft.refill.domain.MyLazyDriverDataModel;
import org.manusoft.refill.domain.TDriverDetailsRf;
import org.manusoft.refill.service.DriverService;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("manageDrivers")
@Scope("session")
public class ManageDrivers implements Serializable {

    private LazyDataModel<TDriverDetailsRf> lazyDataModel;
    private DriverService driverService;
//    private String title;
    private String dlNo;
    private TDriverDetailsRf[] selectedDrivers;

    @Autowired
    public ManageDrivers(DriverService driverService) {
    	System.out.println("created movies");
        this.driverService = driverService;
        lazyDataModel = new MyLazyDriverDataModel(driverService);
    }

    public LazyDataModel<TDriverDetailsRf> getDrivers() {
        return lazyDataModel;
    }

    public void setDrivers(LazyDataModel<TDriverDetailsRf> drivers) {
        this.lazyDataModel = drivers;
    }

    public TDriverDetailsRf[] getSelectedDrivers() {
        return selectedDrivers;
    }

    public void setSelectedDrivers(TDriverDetailsRf[] selectedDrivers) {
        this.selectedDrivers = selectedDrivers;
    }

    public List<String> getDriverbyDLNo(String dlNo) {
        List<TDriverDetailsRf> foundDrivers = driverService.findByDlNoAutoComplete(dlNo);
        List<String> titles = new ArrayList<String>();
        for (TDriverDetailsRf m : foundDrivers) {
            titles.add(m.getDriverDlNo());
        }
        return titles;
    }

    public void search(ActionEvent actionEvent) {
    	lazyDataModel.load(0,1, null, SortOrder.UNSORTED, new HashMap<String, String>() {
            {
                put("driverDlNo", dlNo);
            }
        });
    }

	public String getDlNo() {
		return dlNo;
	}

	public void setDlNo(String dlNo) {
		this.dlNo = dlNo;
	}

	
}